package com.hqyj.controller;

import com.hqyj.exception.R;
import com.hqyj.pojo.PayParameter;
import com.hqyj.service.AliPayService;
import com.hqyj.webSocket.PaySocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;

@RestController
@RequestMapping("/aliPay")
@Slf4j
public class AliPayController {

    @Autowired
    AliPayService aliPayService;

    /**
     * 支付宝异步付款
     * @return
     */
    @PostMapping("/pay")
    public R tranPay(@RequestBody PayParameter payParameter){
        return aliPayService.tranPay(payParameter);
    }

    //支付成功异步回调
//注意：请求方式注解必须PostMapping，不支持GetMapping
    @PostMapping("/returnStatus")
    public void test(HttpServletRequest request) throws IOException {
        //请求状态参数 trade_status，有两个值： 1 WAIT_BUYER_PAY 正在支付 2 TRADE_SUCCESS
        log.info("接收到支付宝发送来支付信息");
        System.out.println("商品订单号："+request.getParameter("out_trade_no"));
        System.out.println("支付宝交易订单号："+request.getParameter("trade_no"));
        System.out.println("订单支付状态："+request.getParameter("trade_status"));

        if(request.getParameter("trade_status").equals("TRADE_SUCCESS")){
            PaySocketServer.SendMessage("ok",request.getParameter("out_trade_no"));
        }
    }

}
